package com.csl.jsonpreference.ui;

import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.csl.jsonpreference.R
import com.csl.jsonpreference.bean.*
import com.csl.jsonpreference.configdes.JsonArrayPreference
import com.csl.jsonpreference.configdes.JsonBasePreference
import com.csl.jsonpreference.configdes.JsonConfigIns
import com.csl.jsonpreference.databinding.ItemArrayPreferenceBinding
import com.csl.jsonpreference.databinding.ItemPreferenceBinding
import com.csl.jsonpreference.util.CLog
import com.csl.ui.base.adapter.QuickAdapterTouchCallback
import org.json.JSONArray
import java.util.*


/**
 * 作者：LIUCHANGSONG on 2020/1/7 10:51
 * 邮箱：cs_lius@126.com
 */
abstract class JsonArrayAdapter(
) : RecyclerView.Adapter<JsonArrayAdapter.JsonItemHolder>() {

    abstract fun onPreferenceSelected(index: Int, data: JSONArray, config: JsonConfigIns?)

    var drapCallback = object : QuickAdapterTouchCallback() {
        override fun onItemMove(fromIndex: Int, toIndex: Int) {
            datas?.let {
                Collections.swap(it.paramDesMap, fromIndex, toIndex)
                var oldData = datas?.jsonData?.get(fromIndex)
                var newData = datas?.jsonData?.get(toIndex)

                datas!!.jsonData.put(toIndex, oldData)
                datas!!.jsonData.put(fromIndex, newData)
                notifyItemMoved(fromIndex, toIndex)
            }
        }

        override fun onRemove(index: Int) {
            removeData(index)
            notifyItemRemoved(index)
        }

    }

    var datas :JsonArrayPreference? = null
//    var datas = ArrayList<JsonConfigIns>()
//    var datas?.jsonData?: JSONArray = JSONArray()
    
//    var checkItemIndex: Int = -1

//    fun getSelectItem():JsonConfigIns? {
//        if(checkItemIndex != -1) {
//            return datas[checkItemIndex]
//        }
//        return null
//    }

    fun addData(data: JsonConfigIns) {
//        datas.add(data)
        datas?.paramDesMap?.add(data)
        notifyDataSetChanged()
    }

    fun resetData(jsonPreference: JsonArrayPreference) {
//        CLog.d("jsonarray", "json数据$jsonData, 列表:$jsonConfig")
        datas = jsonPreference
//        datas?.paramDesMap?.clear()
//        checkItemIndex = -1
//        jsonConfig?.let {
//            datas?.paramDesMap?.addAll(jsonConfig)
//        }
        notifyDataSetChanged()
    }

    fun removeData(index: Int) {
        datas?.paramDesMap?.removeAt(index)
        datas?.jsonData?.remove(index)
//        if(checkItemIndex == index) {
//            checkItemIndex = -1
//        }
        notifyDataSetChanged()
    }

    fun clearData() {
        datas?.paramDesMap?.clear()
//        checkItemIndex = -1
        datas?.jsonData = JSONArray()
        notifyDataSetChanged()
    }

//    fun setCheckedIndex(index: Int) {
//        var oldItem = checkItemIndex
//        if(index == oldItem) {
//            checkItemIndex = -1
//        } else {
//            checkItemIndex = index
//        }
//        if(oldItem != -1) {
//            notifyItemChanged(oldItem)
//        }
//        if(checkItemIndex != -1) {
//            notifyItemChanged(checkItemIndex)
//        }
//    }

    inner class JsonItemHolder(var viewBinding: ItemArrayPreferenceBinding) :
        ViewHolder(viewBinding.root) {
        var nowIndex = -1

        init {
            viewBinding.root.setOnClickListener {
                var data =  datas!!.paramDesMap[nowIndex]
                onPreferenceSelected(nowIndex, datas!!.jsonData, data)
//                setCheckedIndex(nowIndex)
//                if(checkItemIndex != -1) {
//                    onPreferenceSelected(nowIndex, datas?.jsonData?, data)
//                } else {
//                    onPreferenceSelected(-1, datas?.jsonData?, null)
//                }
            }
        }

        fun setData(index: Int, config: JsonConfigIns) {
            var title = if (TextUtils.isEmpty(config.title)) {
                config.key
            } else {
                config.title
            }
            if(TextUtils.isEmpty(title)) {
                viewBinding.jsonTvTitle.visibility = View.GONE
            } else {
                viewBinding.jsonTvTitle.visibility = View.VISIBLE
                viewBinding.jsonTvTitle.text = "$title"
            }
            viewBinding.jsonTvNum.text = "$index :"

            if (TextUtils.isEmpty(config.describe)) {
                viewBinding.jsonTvDes.visibility = View.GONE
            } else {
                viewBinding.jsonTvDes.visibility = View.VISIBLE
                viewBinding.jsonTvDes.text = config.describe
            }

            when (config.configType) {
                ConfigTYPE.BOOLEAN -> {
                    viewBinding.jsonTvValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonCbValue.visibility = View.VISIBLE
                    viewBinding.jsonCbValue.isChecked = datas!!.jsonData.optBoolean(
                        index,
                        false
                    )
//                    viewBinding.jsonCbValue.isChecked = datas?.jsonData?.optBoolean(
//                        config.key,
//                        false
//                    )
                }

                ConfigTYPE.COLOR -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.VISIBLE
                    viewBinding.jsonTvValue.visibility = View.GONE
                    viewBinding.jsonVColor.setBackgroundColor(
                        datas!!.jsonData.optInt(
//                            config.key,
                            index,
                            viewBinding.jsonTvDes.context.resources.getColor(R.color.colorPrimary)
                        )
                    )
                }

                ConfigTYPE.ENUM -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    if (config is JsonBasePreference) {
                        CLog.d("enum", "枚举类型:${config.classType}")
                        viewBinding.jsonTvValue.text = datas?.jsonData?.optString(
//                            config.key,
                            index,
                            ""
                        )
                    } else {
                        viewBinding.jsonTvValue.text = datas?.jsonData?.optString(
//                            config.key,
                            index,
                            ""
                        )
                    }
//                    viewBinding.jsonTvValue.text = datas?.jsonData?.op
                }
                ConfigTYPE.OBJECT, ConfigTYPE.ARRAY_OBJECT-> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    var valueStr = config.getParseValueStr()

                    if(TextUtils.isEmpty(valueStr)) {
                        viewBinding.jsonTvValue.visibility = View.GONE
                    } else {
                        viewBinding.jsonTvValue.visibility = View.VISIBLE
                        viewBinding.jsonTvValue.text = valueStr
                    }
                }

                else -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    viewBinding.jsonTvValue.text = datas?.jsonData?.optString(
//                        config.key,
                        index,
                        ""
                    )
                }
            }
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JsonItemHolder {
        var inflater = LayoutInflater.from(parent.context)
        return JsonItemHolder(ItemArrayPreferenceBinding.inflate(inflater, parent, false))
    }

    override fun onBindViewHolder(holder: JsonItemHolder, position: Int) {
        var config =  datas!!.paramDesMap[position]
        holder.nowIndex = position
        holder.setData(position, config)
    }

    override fun getItemCount(): Int {
        return  datas?.paramDesMap?.size?:0
    }

    override fun getItemViewType(position: Int): Int {
//        return datas[position].type.ordinal
        return 0
    }


}
